import { defineStore } from "pinia";
import provider from "@/assets/document/provider.json";
export const settingsStore = defineStore("settings", () => {
  const isDark = ref<boolean>(false);
  const getDark = computed(() => isDark);
  const setDark = (value: boolean) => {
    isDark.value = value;
  };
  const settings = ref<SettingsMenuValues>({
    general: {},
    function: {},
    aiSetting: {
      value: provider,
      nowProviderName: "Openai",
    },
    display: {
      layout: "compact",
    },
  });
  const modalSenderOptions = computed(() => {
    const result: selectAIOptions[] = [];
    for (const provider of settings.value.aiSetting.value) {
      if (!provider?.MODELS || provider.MODELS.length === 0) continue;
      provider.MODELS.reduce((pre, cur) => pre + (cur.active ? 1 : 0), 0) &&
        result.push({
          label: provider.PROVIDES,
          value: provider.PROVIDES,
          disabled: true,
        });

      for (const model of provider.MODELS) {
        model.active &&
          result.push({
            label: model.id,
            value: { model, provider },
          });
      }
    }
    return result;
  });
  const getAllSettings = computed({
    get: () => settings.value,
    set: () => {
      throw new Error("Cannot set readonly property");
    },
  });
  const setSetting = <K extends SettingsFormKey>(
    key: K,
    field: keyof SettingsMenuValues[K],
    value: SettingsMenuValues[K][keyof SettingsMenuValues[K]]
  ) => {
    settings.value[key][field] = value;
  };
  const getSetting = <K extends SettingsFormKey>(
    key: K,
    field: keyof SettingsMenuValues[K]
  ) => {
    return settings.value[key][field];
  };
  const addProvider = (PROVIDES: PROVIDES) => {
    settings.value.aiSetting.value.push({
      API_KEY: "",
      API_URL: "",
      DEFAULT_URL: "",
      MODELS: [],
      PROVIDES,
      ICON_URL: "",
      ICON_WIDTH: 0,
    });
  };
  const createModel = (model: Partial<ModelExtend>) => {
    const now = Date.now();
    const result: ModelExtend = {
      id: "",
      created: now,
      owned_by: "",
      object: "model",
      name: "",
      active: true,
      key: `id-${uuidV4()}`,
      provider: "Openai",
    };
    for (const key in model) {
      if (model.hasOwnProperty(key) && key in result) {
        const typedKey = key as keyof ModelExtend;
        const value = model[typedKey];
        if (value !== undefined) {
          result[typedKey] = value as never;
        }
      }
    }
    return result;
  };
  const addModel = (provider: PROVIDES) => {
    settings.value.aiSetting.value
      .find((item) => item.PROVIDES === provider)
      ?.MODELS.push(createModel({ owned_by: provider, provider }));
  };
  const getNowProviderSetting = () => {
    return settings.value.aiSetting.value.find(
      (item) => item.PROVIDES === settings.value.aiSetting.nowProviderName
    );
  };
  const getNowProviderSettingIndex = computed(() => {
    return settings.value.aiSetting.value.findIndex(
      (item) => item.PROVIDES === settings.value.aiSetting.nowProviderName
    );
  });
  const getProviderSettingByLabel = (label: PROVIDES) => {
    return settings.value.aiSetting.value.find(
      (item) => item.PROVIDES === label
    );
  };
  const setNowProviderSetting = <K extends keyof ProviderValue>(
    key: K,
    provider: ProviderValue[K]
  ) => {
    const index = settings.value.aiSetting.value.findIndex(
      (item) => item.PROVIDES === settings.value.aiSetting.nowProviderName
    );
    if (index !== -1) {
      settings.value.aiSetting.value[index][key] = provider;
    }
  };
  const AIBarChatNowSelectModel = ref<ModelExtend>();
  const getAllModels = () => {
    const result: ModelExtend[] = [];
    for (const provider of settings.value.aiSetting.value) {
      if (!provider?.MODELS || provider.MODELS.length === 0) continue;
      for (const model of provider.MODELS) {
        model.active && result.push(model);
      }
    }
    return result;
  };

  const setAiBarChatNowSelectModel = ({
    model,
  }: {
    model: ModelExtend;
    provider: ProviderValue;
  }) => {
    AIBarChatNowSelectModel.value = model;
  };

  const getAIBarChatNowSelectModel = () => AIBarChatNowSelectModel.value!;
  const getProviderByName = (name: string) => {
    return settings.value.aiSetting.value.find(
      (item) => item.PROVIDES === name
    );
  };
  const getLayout = computed(() => {
    return settings.value.display.layout;
  });
  const panesSize = ref([15, 55, 30]);
  const setPanesSize = (size: number[]) => {
    panesSize.value = size;
  };
  const setChatPanelSize = (size: number) => {
    panesSize.value[2] = size;
    panesSize.value[1] = 100 - size - panesSize.value[0];
  };
  const openAIPanel = computed(() => {
    return panesSize.value[2] > 1;
  });
  return {
    setChatPanelSize,
    openAIPanel,
    panesSize,
    setPanesSize,
    getProviderByName,
    getAIBarChatNowSelectModel,
    getNowProviderSettingIndex,
    setAiBarChatNowSelectModel,
    getAllModels,
    isDark,
    settings,
    setDark,
    getDark,
    getAllSettings,
    setSetting,
    getSetting,
    getNowProviderSetting,
    modalSenderOptions,
    AIBarChatNowSelectModel,
    setNowProviderSetting,
    getLayout,
    addProvider,
    getProviderSettingByLabel,
    addModel,
  };
});
